<template>
  <div class="channel-edit">
    <van-cell :border="false">
      <div slot="title" class="title-text">我的频道</div>
      <van-button @click="isEditShow = !isEditShow" size="mini" round type="danger" plain>{{isEditShow?'完成':'编辑'}}</van-button>
    </van-cell>

    <van-grid class="my-grid" :gutter="10">
      <van-grid-item class="channel-item" v-for="(channelItem, index) in channels" :key="index" >
        <template #text>
            <span @click="onMyChannelClick(index,channelItem.id)" class="text" :class="{active: active === index}" slot="text">{{ channelItem.name }}</span>
        </template>
        <template #icon>
            <van-icon name="close" v-show="isEditShow && index != 0"></van-icon>
        </template>
      </van-grid-item>
    </van-grid>

    <van-cell :border="false">
      <div slot="title" class="title-text">频道推荐</div>
    </van-cell>

    <van-grid :gutter="10">
      <van-grid-item
      class="channel-item"
      v-for="value in recommendChannels"
      :key="value.id"
      :text="value.name"
      icon="plus"
      @click="onAddChannel(value)"
      />
    </van-grid>
  </div>
</template>

<script>
import { getAllChannelsApi, addUserChannelApi, deleteUserChannelApi } from '@/api/index.js'
import { setItem } from '@/utils/storage.js'
export default {
  name: 'ChannelEdit',
  props: {
    channels: {
      type: Array,
      default: () => []
    },
    active: {
      type: Number,
      default: 0
    }
  },
  data () {
    return {
      allChannels: [], // 所有频道
      channelsClone: this.channels, // 拷贝父组件传过来的值
      isEditShow: false // 编辑 完成 按钮

    }
  },
  created () {
    // 网络请求
    this.loadAllChannels()
  },
  computed: {
    recommendChannels () {
      // 过滤实现找到没有id 的
      const list = this.allChannels.filter(obj => {
        const findIndex = this.channels.findIndex(channel => {
          return obj.id === channel.id
        })
        // 如果返回值为-1那么就是不存在我的频道
        return findIndex === -1
      })
      return list
    }
  },
  methods: {
    // 拿到全部的频道
    async loadAllChannels () {
      try {
        const { data: res } = await getAllChannelsApi()
        console.log(res.data.channels)
        this.allChannels = res.data.channels
      } catch (error) {
        this.$toast('获取频道列表数据失败')
      }
    },
    // 添加频道
    onAddChannel (channel) {
      // 不可以直接修改父组件传递过来的参数，所以拷贝一份在data中
      this.channelsClone.push(channel)
      // 数据持久化 判断是否登录状态
      if (this.$store.state.user.token) {
        // 是登录状态  调接口保存当前的编辑频道信息
        this.addChannels(channel)
      } else {
        // 未登录 将频道状态保存到本地存储
        setItem('TOUTIAO_CHANNELS', this.channels)
      }
    },
    // 点击我的频道进行操作
    onMyChannelClick (index, id) {
      if (this.isEditShow) {
        // 编辑状态
        if (index === 0) return
        this.channelsClone.splice(index, 1)
        // 删除当前选中之前的频道时 重新计算选中位置
        if (this.active > index) {
          this.$emit('update-active', this.active - 1, true) // 修改当前频道 true不让弹窗关闭
        }
        // 数据持久化 删除操作
        if (this.$store.state.user.token) {
          // 是登录状态  调接口保存当前的编辑频道信息
          console.log(1)
          this.deleteChannels(id)
        } else {
          console.log(2)
          // 未登录 将频道状态保存到本地存储
          setItem('TOUTIAO_CHANNELS', this.channels)
        }
      } else {
        // 完成状态
        this.$emit('update-active', index) // 修改当前频道
      }
    },
    async addChannels (channel) {
      try {
        await addUserChannelApi({
          id: channel.id,
          seq: this.channels.length - 1
        })
        this.$toast.success('添加成功')
      } catch (error) {
        console.log(error)
        this.$toast.fail('添加成功')
      }
    },
    // 持久化删除操作
    async deleteChannels (id) {
      try {
        await deleteUserChannelApi(id)
        this.$toast.success('添加成功')
      } catch (error) {
        this.$toast.fail('添加失败')
      }
    }
  }
}
</script>
<style scoped lang="less">
.channel-edit {
  .title-text {
    height: 100%;
    line-height: 58px;
    font-size: 32px;
    color: #333333;
  }

  .edit-btn {
    line-height: 48px;
    width: 104px;
    height: 48px;
    font-size: 26px;
    color: palevioletred;
    border: 1px solid palevioletred;
  }

  .channel-item {
    height: 86px;
    width: 160px;

    /* 设置按钮背景 */
    /deep/ .van-grid-item__content {
      background-color: #f5f5f6;

      /* 设置按钮字体样式 */
      .van-grid-item__text,
      .text {
        color: #222;
        font-size: 26px;
        margin-top: 0;
      }
      .van-grid-item__icon-wrapper {
        font-size: 28px;
        position: absolute;
        top: -8px;
        right: -8px;
        color: #cacaca;
      }
      .active {
        color: palevioletred;
      }
    }
  }

  /deep/ .recommend-grid {
    .van-grid-item__content {
      white-space: nowrap;
      flex-direction: row;

      /* 设置按钮字体样式 */

      .van-icon-plus {
        font-size: 28px;
        margin-right: 6px;
      }
    }
  }

  /deep/ .my-grid {
    .van-grid-item__content {
      .van-icon-close {
        position: absolute;
        right: -10px;
        top: -10px;
        font-size: 30px;
        color: #cacaca;
        z-index: 2;
      }
    }
  }
}
</style>
